<template>
    <el-card shadow="never" class="border-0">
        <ListHeader layout="create,refresh" @refresh="getData" @create="handleCreate" />
        <el-tree :data="tableData" :props="{ label: 'name', children: 'child' }" v-loading="loading" node-key="id">
            <template #default="{ node, data }">
                <div class="custom-tree-node">
                    <span>{{ data.name }}</span>
                </div>
                <div class="ml-auto">
                    <el-button type="primary" size="small" text @click.stop="openGoodsDrawer(data)" :loading="data.goodsDrawerLoading">推荐商品</el-button>
                    <el-switch :modelValue="data.status" :active-value="1" :inactive-value="0"
                        @change="handleStatusChange($event, data)" :loading="data.statusLoading">
                    </el-switch>
                    <el-button type="primary" size="small" text @click.stop="handleEdit(data)">修改</el-button>
                    <el-popconfirm title="是否要删除该记录？" confirm-button-text="确定" cancel-button-text="取消"
                        @confirm.stop="handleDelete(data.id)">
                        <template #reference>
                            <el-button type="primary" size="small" text>删除</el-button>
                        </template>
                    </el-popconfirm>
                </div>
            </template>
        </el-tree>
    </el-card>
    <FormDrawer ref="formDrawerRef" :title="drawerTitle" @submit="handleSubmit">
        <el-form :model="form" ref="formRef" :rules="rules" label-width="120px" :inline="false">
            <el-form-item label="分类名称" prop="name">
                <el-input v-model="form.name" placeholder="输入分类名称"  clearable ></el-input>
            </el-form-item>
        </el-form>
    </FormDrawer>

    <GoodsDrawer ref="GoodsDrawerRef"/>
</template>

<script setup>
import { ref } from 'vue';
import ListHeader from '~/components/ListHeader.vue';
import FormDrawer from "~/components/FormDrawer.vue";
import GoodsDrawer from './components/GoodsDrawer.vue';
import { getCategoryList, createCategory, editCategory, deleteCategory, upateCategoryStatus,getCategoryGoods } from "~/api/category.js"
import { useInitTab, useInitForm } from "~/composables/useCommon.js"
const { loading, tableData, getData, handleStatusChange } = useInitTab({
    getList: getCategoryList,
    onGetListSuccess: res => {
        tableData.value = res.map( o => {
            o.goodsDrawerLoading = false
            return o
        })
    },
    updateStatus: upateCategoryStatus
});
const { formDrawerRef, formRef, form, drawerTitle, rules, handleSubmit, handleCreate, handleEdit, handleDelete } = useInitForm({
    form: {
        name:""
    },
    rules: {},
    loading,
    getData,
    edit: editCategory,
    create: createCategory,
    delete: deleteCategory
});

const GoodsDrawerRef = ref(null);

const openGoodsDrawer = (data) => GoodsDrawerRef.value.open(data)

</script>

<style>
.custom-tree-node {
    @apply flex-1 flex items-center text-sm pr-2;
}

.el-tree-node__content {
    padding: 20px 0;
}
</style>